package com.itfreer.data.app;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itfreer.data.app.entity.AppVersionEntity;
import com.itfreer.form.api.BaseRestService;
import com.itfreer.form.api.BaseService;
import com.itfreer.form.api.PageInfo;
import com.itfreer.utils.ResponseEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 定义APP版本管理Rest服务接口
 */
@RestController
@RequestMapping("/appversion")
@Api("APP版本管理")
public class RestAppVersionService extends BaseRestService<AppVersionEntity> {

	@Autowired(required = false)
	private AppVersionService service;

	@Override
	protected BaseService<AppVersionEntity> getService() {
		return service;
	}

	/**
	 * 获取是否需要更新版本
	 */
	@ApiOperation(value = "获取是否需要更新版本")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "appid", value = "app编号", paramType = "path", dataType = "String", required = true),
			@ApiImplicitParam(name = "version", value = "版本号", paramType = "path", dataType = "int", required = true) })
	@PostMapping(value = "needupdate/{appid:.+}/{version:.+}", produces = "application/json")
	public ResponseEntity<AppVersionEntity> needupdate(@PathVariable("appid") String appid, @PathVariable Integer version) {
		PageInfo pInfo = new PageInfo();
		pInfo.setPageSize(1);
		pInfo.setPageIndex(1);
		Map<String, Object> where = new HashMap<>();
		where.put("infoid", appid);
		where.put("numversion:>", version);
		pInfo.setWhere(where);
		Map<String, Integer> order = new HashMap<>();
		order.put("numversion", 1);
		pInfo.setOrder(order);
		List<AppVersionEntity> entitys = getService().getEntitys(pInfo);
		if (entitys == null || entitys.size() <= 0) {
			ResponseEntity<AppVersionEntity> result = new ResponseEntity<>();
			result.setCode("1");
			result.setMsg("无需更新");
			return result;
		} else {
			return new ResponseEntity<>(entitys.get(0));
		}
	}
}
